#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
python 学习
"""

__author__ = 'hubert'

import pymysql


def insert():
    conn = pymysql.connect('127.0.0.1', 'root' , 'pass' , 'python')

    cursor = conn.cursor()

    sql = "insert into student(name,age,sex,create_time,update_time) \
            values('%s','%s','%s','%s','%s')" %  \
          ('jan', '16', '女', '2019-04-11 14:51:39', '2019-04-11 14:51:50')

    try:
        # 执行SQL
        cursor.execute(sql)

        # 提交
        conn.commit()
    except:
        # 发生错误时回滚
        conn.rollback()

    # 关闭数据库
    conn.close()



def query():
    conn = pymysql.connect(host='127.0.0.1',user='root',passwd='pass',db='python',port=3306,charset='utf8')
    # 使用cursor()方法获取游标操作
    cursor = conn.cursor()

    # SQL
    sql = "select * from student where age > 15"

    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 获取所有记录列表
        results = cursor.fetchall()

        for row in results:
            id = row[0]
            name = row[1]
            age = row[2]
            sex = row[3]
            # 打印结果
            print("id=%s,name=%s,age=%s,sex=%s"  % (id, name, age, sex))
    except:
        print("Error: unable to fetch data")

    # 关闭数据库
    conn.close()

if __name__ == '__main__':
    # query()
    insert()